package com.questetra.OAuth.core
{
    public class QueryString
    {
        public function QueryString(queryString:String = "")
        {
            if (queryString.length)
            {
                var pairs:Array = queryString.split("&");
                for (var n:int = 0; n < pairs.length; n++)
                {
                    var pair:String = pairs[n];

                    var values:Array = pair.split("=");
                    if (values.length == 2)
                    {
                        add(new Parameter(values[0], decodeURIComponent(values[1])));
                    }
                    else
                    {
                        throw new Error("Invalid parameter found. There was not a valid key/value pair, instead we got '" + pair + "'");
                    }
                }
            }
        }
        
        public function add(param:Parameter):void
        {
            params.push(param);
            paramsAssociative[param.key] = param.value;
        }
        

        public function get length():uint
        {
            return params.length;
        }
        
        public function getValue(key:String):String
        {
            var ret:String = paramsAssociative[key];
            return ret != null ? ret : "";
        }
        
        public function getParam(index:uint):Parameter
        {
            return params[index];
        }

        
        public function sort():void
        {
            function paramsCompareFunction(a:Parameter, b:Parameter):Number
                {
                    // Sort on key and then value...
                    if (a.key == b.key)
                    {
                        if (a.value == b.value)
                        {
                            return 0;
                        }

                        return (a.value < b.value) ? -1 : 1;
                    }
                    
                    return (a.key < b.key) ? -1 : 1;
                };
            params.sort(paramsCompareFunction);
        }
        

        public function toString(signed_value:Boolean = true):String
        {
            var ret:String = "";
            for (var n:int = 0; n < params.length; n++)
            {
                var param:Parameter = params[n];

                if (ret.length) ret += "&";
                if (signed_value)
                {
                    ret += param.key + "=" + param.valueEncoded;
                }
                else
                {
                    ret += param.key + "=" + param.value;
                }
            }
            return ret;
        }
        
        public function toPostObject():Object
        {
            var ret:Object = new Object();
            for (var n:int = 0; n < params.length; n++)
            {
                var param:Parameter = params[n];
                ret[param.key] = param.value;
            }
            return ret;
        }
        
        
        
        private var params:Array = new Array;
        private var paramsAssociative:Object = new Object;
    }
}